package com.yc.project1.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author zwei
 * @create 2024-08-04 16:29
 */
@Component
@Aspect
public class LogAspect {
    @Pointcut("execution(* com.yc.project1.service.*.update*(..))") //匹配业务层中更新操作
    private void a(){}  //切入点的签名
    @Pointcut("execution(* com.yc.project1.service.*.find*(..))||execution(* com.yc.project1.service.*.show*(..))")   //匹配业务层中查询操作
    private void b(){}
    @Pointcut("a()||b()")
    private void c(){}

    @Before("b()")
    public void doAccessCheck(){
        System.out.println("权限验证");
    }
    @AfterReturning(pointcut = "a()",returning = "returnResult")
    public void deLog(Object returnResult){
        System.out.println("记录日志");
        System.out.println(new Date());
        System.out.println("切面拦截的执行结果为："+returnResult);
    }
    @AfterThrowing(pointcut = "a()",throwing = "ex")
    public void deException(ArithmeticException ex){
        System.out.println("出异常了");
        System.out.println("异常信息为："+ex.getMessage());
        System.out.println("异常结束");
    }
    @Around("c()")
    public Object doRecordTime(ProceedingJoinPoint pjp){
        long start=System.currentTimeMillis();
        //目标方法运行
        Object returnObject=null;
        try {
            returnObject=pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        long end=System.currentTimeMillis();
        System.out.println("方法运行时间为："+(end-start));
        return returnObject;
    }
}
